package org.wak.action.actions
{
	import org.wak.action.IInteractable;
	import org.wak.character.display.AbstractCharacterDisplay;
	import org.wak.main.AdventureMaster;
	import org.wak.main.PuzzleControllerBase;
	import org.wak.utils.IIterator;
	
	public class GiveAction extends BaseAction
	{
		private var mParam2:IInteractable = null ;
		private var mGiveConnector:String = "" ;
		
		public function GiveAction()
		{
			super();
			mActionId = AdventureActions.ActionIdGive ;
			mActionText = AdventureMaster.Texts.GiveText ;
			mGiveConnector = AdventureMaster.Texts.GiveConnector ;
		}
		
		public override function AddParameter(pItem:IInteractable):void
		{
			
			if( mParam1 != null )
			{
				mParam2 = pItem ;
			}
			
			if( mParam1 == null )
			{
				mParam1 = pItem ;
			}
				
			
		}
		
		public override function TestParameter(pItem:IInteractable):Boolean
		{
			if( mParam1 == null )
			{
				return true ;
			}
			else
			{
				if( pItem is AbstractCharacterDisplay )
				{
					return true ;
				}
			}
			
			return false ;
		}
		
		public override function get isReady():Boolean
		{
					
			if((mParam1 != null) && (mParam2 != null))
			{
				return true;
			}
			
			return false;
		}
		
		public override function GetText():String
		{
			var fRetText:String = mActionText ;
			
			if( mParam1 != null)
				fRetText += " " + mParam1.interactionName + " " + mGiveConnector  ; 
				
			if( mParam2 != null)
				fRetText += " " + mParam2.interactionName ;
			
			return fRetText ;
		}
		
		public override function Execute(pIterator:IIterator):Boolean 
		{
			var fMethodFound:Boolean = false ;
			
			var fPuzzleController:PuzzleControllerBase = null;
			
			var fMethodEventName:String = "" ;
			
			
			fMethodEventName = mActionId+mParam1.interactableId+"To"+mParam2.interactableId ;
			
			while( pIterator.hasNext() )
			{
				fPuzzleController = pIterator.next() as PuzzleControllerBase ;
				fMethodFound = fMethodFound || fPuzzleController.hasOwnProperty(fMethodEventName) ;
				if( fMethodFound )
				{
					fPuzzleController[fMethodEventName](mParam1,mParam2);
				}
			}
			
			return fMethodFound
		}
		
		public override function CanExecute(pIterator:IIterator):Boolean 
		{
			var fMethodFound:Boolean = false ;
			
			var fPuzzleController:PuzzleControllerBase = null;
			
			var fMethodEventName:String = "" ;
			
			fMethodEventName = mActionId+mParam1.interactableId+"To"+mParam2.interactableId ;
			
			
			while( pIterator.hasNext() )
			{
				fPuzzleController = pIterator.next() as PuzzleControllerBase ;
				fMethodFound = fMethodFound || fPuzzleController.hasOwnProperty(fMethodEventName) ;
				if( fMethodFound )
				{
					return true
				}
			}
			
			return false ;
		}
		
		public override function GetNoActionText():String
		{
			return AdventureMaster.Texts.GiveNoAction ;
		} 
				
		/** @inheritDoc		 */
		public override function GetParam2():IInteractable 
		{
			return mParam2 ;
		}
		
		/** @inheritDoc		 */
		public override function get paramsNumber():int
		{
			return 2 ;
		}
		
	}
}